// pages/personal/personal.js
const config = require('../../config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfos: {},
    phone: '',
    maskShow: true,
    phoneInfo:{}
  },
  // 实名认证
  goAuth:function () {
    wx.navigateTo({
      url: '/pages/authentication/authentication'
    })
  },
  // 显示手机号弹窗
  changeMask: function(){
    this.setData({
      maskShow: true,
    })
  },
  // 拿到手机号值
  phoneValue: function (e) {
    this.setData({
      phone: e.detail.value,
    })
    // console.log(this.data.phone,"phone")
  },
  // 验证手机号
  isPhone: function (e) {
    var n = /^[1][3,4,5,7,8][0-9]{9}$/
    if (this.data.userInfos.mobile == null){
      if (this.data.phone == '') {
        wx.showToast({
          title: '请输入您的手机号',
          icon: 'none',
        })
        this.setData({
          maskShow: true,
        })
      } else if (!n.test(this.data.phone)) {
        wx.showToast({
          title: '请填写正确的手机号',
          icon: 'none',
        })
        this.setData({
          phone: '',
          maskShow: true,
        })
      }
    }else{
      if ((this.data.phone == '') || (!n.test(this.data.phone)) ) {
        this.setData({
          maskShow: false,
          phone: this.data.userInfos.mobile,
        })
      }
    }
    
  },
  // 提交手机号
  subMask: function () {
    console.log(this.data.userInfos, "this.data.userInfos")
    // console.log(this.data.phone,"phone")
    this.isPhone();
    var that = this;
    if (this.data.phone != ''){
      wx.request({
        url: config.userInfo,
        method: 'PUT',
        data: {
          username: this.data.userInfos.username,
          avatar: this.data.userInfos.avatar,
          sex: this.data.userInfos.sex,
          mobile: this.data.phone,
        },
        header: {
          'content-type': 'application/json',
          'Authorization': wx.getStorageSync('token_type') + ' ' + wx.getStorageSync('access_token'),
        },
        success: function (res) {
          console.log(res, "userInfores")
          that.setData({
            userInfos: res.data.data,
            maskShow: false,
          })
        }
      })
    }
  },
  getPhoneNumber: function (e) {
    var that = this;
    if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
      this.setData({
        maskShow:true
      })
    } else {
      this.setData({
        maskShow: false
      })

      wx.request({
        url: config.jieMi,
        method: 'POST',
        data: {
          iv: e.detail.iv,
          encryptedData: e.detail.encryptedData,
        },
        header: {
          'content-type': 'application/json',
          'Authorization': wx.getStorageSync('token_type') + ' ' + wx.getStorageSync('access_token'),
        },
        success: function (res) {
         
          that.data.phoneInfo = JSON.parse(res.data.data)
          console.log(that.data.phoneInfo);
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.setData({
    //   userInfos: wx.getStorageSync('userInfo')
    // })
    wx.showLoading({
      title: '加载中',
    })
    this.getUserinfo();
  },

  // 拿到已有用户信息
  getUserinfo: function(){
    var that = this;
    wx.request({
      url: config.initUser,
      method: 'GET',
      data: {
      },
      header: {
        'content-type': 'application/json',
        'Authorization': wx.getStorageSync('token_type') + ' ' + wx.getStorageSync('access_token'),
      },
      success: function (res) {
        wx.hideLoading();
        that.setData({
          userInfos: res.data.data,
        })
        // console.log(res, "userInfores")
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})